#include <stdio.h>

#define UNITY_INCLUDE_CONFIG_H
#include "unity.h"
#include "ringbuffer.h"

void setUp(void)
{}
void tearDown(void)
{}

void test_ringbuffer_create(void)
{
    ringbuffer_t *rg = ringbuffer_create(10);
    TEST_ASSERT_NOT_NULL(rg);
    ringbuffer_destroy(rg);

    rg = ringbuffer_create(0);
    TEST_ASSERT_NULL(rg);
    ringbuffer_destroy(rg);

    rg = ringbuffer_create(1000);
    TEST_ASSERT_NOT_NULL(rg);
    ringbuffer_destroy(rg);
}

void test_ringbuffer_empty_and_full(void)
{
    uint8_t buffer[20] = {0};
    ringbuffer_t *rg = ringbuffer_create(10);
    TEST_ASSERT_NOT_NULL(rg);

    TEST_ASSERT_TRUE(ringbuffer_empty(rg));
    TEST_ASSERT_FALSE(ringbuffer_full(rg));
    TEST_ASSERT_EQUAL_UINT32(0, ringbuffer_used(rg));
    TEST_ASSERT_EQUAL_UINT32(10, ringbuffer_remain(rg));

    TEST_ASSERT_EQUAL_UINT32(10, ringbuffer_put(rg, buffer, 20));
    TEST_ASSERT_FALSE(ringbuffer_empty(rg));
    TEST_ASSERT_TRUE(ringbuffer_full(rg));
    TEST_ASSERT_EQUAL_UINT32(10, ringbuffer_used(rg));
    TEST_ASSERT_EQUAL_UINT32(0, ringbuffer_remain(rg));

    ringbuffer_clear(rg);

    TEST_ASSERT_TRUE(ringbuffer_empty(rg));
    TEST_ASSERT_FALSE(ringbuffer_full(rg));
    TEST_ASSERT_EQUAL_UINT32(0, ringbuffer_used(rg));
    TEST_ASSERT_EQUAL_UINT32(10, ringbuffer_remain(rg));

    ringbuffer_destroy(rg);
}

void test_ringbuffer_use(void)
{
    uint8_t buffer[20] = {0};
    ringbuffer_t *rg = ringbuffer_create(10);
    TEST_ASSERT_NOT_NULL(rg);

    TEST_ASSERT_TRUE(ringbuffer_empty(rg));
    TEST_ASSERT_FALSE(ringbuffer_full(rg));
    TEST_ASSERT_EQUAL_UINT32(0, ringbuffer_used(rg));
    TEST_ASSERT_EQUAL_UINT32(10, ringbuffer_remain(rg));

    TEST_ASSERT_EQUAL_UINT32(2, ringbuffer_put(rg, buffer, 2));
    TEST_ASSERT_EQUAL_UINT32(0, ringbuffer_put(rg, buffer, 0));
    TEST_ASSERT_EQUAL_UINT32(5, ringbuffer_put(rg, buffer, 5));
    TEST_ASSERT_EQUAL_UINT32(3, ringbuffer_put(rg, buffer, 4));
    TEST_ASSERT_EQUAL_UINT32(0, ringbuffer_put(rg, buffer, 4));
    TEST_ASSERT_EQUAL_UINT32(4, ringbuffer_put_cover(rg, buffer, 4));

    TEST_ASSERT_EQUAL_UINT32(3, ringbuffer_get(rg, buffer, 3));
    TEST_ASSERT_EQUAL_UINT32(0, ringbuffer_get(rg, buffer, 0));
    TEST_ASSERT_EQUAL_UINT32(7, ringbuffer_get(rg, buffer, 10));
    TEST_ASSERT_EQUAL_UINT32(0, ringbuffer_get(rg, buffer, 2));

    ringbuffer_destroy(rg);
}
